package com.cskaoyan.javase.homework.day04._03array.merge;

import java.util.Arrays;

/**
 * 合并数组,思路是：
 *      首先获取两个数组的长度得出新数组的长度
 *
 * @since 16:21
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class MergeArrDemo {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 6, 7, 8};
        int[] arr2 = {4, 5, 6};
        int[] newArr = mergeTwoIntArr(arr1, arr2);
        System.out.println(Arrays.toString(newArr));
    }

    /**
     * 合并两个int数组,得到一个新数组
     * @since 16:33
     * @param arr1 需要合并的数组1
     * @param arr2 需要合并的数组2
     * @return int[]
     * @author wuguidong@cskaoyan.onaliyun.com
     */
    public static int[] mergeTwoIntArr(int[] arr1, int[] arr2) {
        int mergeLength = arr1.length + arr2.length;
        int[] result = new int[mergeLength];

        // 设置一个初始下标用于给结果数组赋值
        int index = 0;

        // 开始赋值arr1
        for (int i = 0; i < arr1.length; i++) {
            result[index] = arr1[i];
            index++;
        }

        // 开始赋值arr2
        for (int i = 0; i < arr2.length; i++) {
            result[index] = arr2[i];
            index++;
        }
        return result;
    }

}
